//
//  PanPopupConfirmViewController.m
//  CreditHub
//
//  Created by chenglang on 2024/9/21.
//

#import "PanPopupConfirmViewController.h"
#import "PanPopupConfirmInputCell.h"
#import "CHPopUpViewController.h"
#import "HomeRequest.h"

@interface PanPopupConfirmViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PanPopupConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.startTime = [BaseService getTimeStringSince1970];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PanPopupConfirmInputCell" bundle:nil] forCellReuseIdentifier:@"PanPopupConfirmInputCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrayInfo.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PanPopupConfirmInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PanPopupConfirmInputCell"];
    cell.labelTitle.text = self.arrayInfo[indexPath.row][@"firmly"];
    cell.textInput.text = self.arrayInfo[indexPath.row][@"models"];
    cell.textInput.tag = indexPath.row  + 1000;
    cell.textInput.delegate = self;
//    [cell.buttonCard addTarget:self action:@selector(clickCard:) forControlEvents:UIControlEventTouchUpInside];
    if(indexPath.row ==2){
        cell.button.hidden = NO;
        [cell.button addTarget:self action:@selector(clickPan) forControlEvents:UIControlEventTouchUpInside];
    }else{
        cell.button.hidden = YES;
    }
//    [cell.buttonPan addTarget:self action:@selector(clickPan:) forControlEvents:UIControlEventTouchUpInside];
//    cell.dicHumming = self.dicHumming;
    return cell;
}
- (void)clickPan{
    
    NSInteger index = 2;
    CHPopUpViewController *pop = [[CHPopUpViewController alloc] init];
    pop.startDate = self.arrayInfo[index][@"models"];
    pop.modalPresentationStyle = UIModalPresentationOverFullScreen;
    __weak typeof(self) weakSelf =self;
    pop.okblock = ^(NSString * _Nonnull date) {
      
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:weakSelf.arrayInfo[index]];
        [dic setObject:date forKey:@"models"];
        [weakSelf.arrayInfo replaceObjectAtIndex:index withObject:dic];
        [weakSelf.tableView reloadData];
    };
    [self presentViewController:pop animated:YES completion:nil];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSInteger row = textField.tag - 1000;
    NSMutableDictionary *dic =  [NSMutableDictionary dictionaryWithDictionary:self.arrayInfo[row]];
    [dic setValue:textField.text forKey:@"models"];
    [self.arrayInfo replaceObjectAtIndex:row withObject:dic];
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

- (IBAction)clickOK:(id)sender {
    [self.view endEditing:YES];
    
    
    NSMutableDictionary *dicparams = [[NSMutableDictionary alloc] init];
    [dicparams setValue:self.lightat forKey:@"lightat"];
    for(NSDictionary *dic in self.arrayInfo){
        [dicparams setValue:CHString(dic[@"models"]) forKey:CHString(dic[@"ofexcitement"])];
    }
    [self reqeustSaveInfo:dicparams];
    
   
}

- (void)reqeustSaveInfo:(NSDictionary *)dicInfo{
    xhl_hudLoading();
    __weak typeof(self) weakSelf = self;
    
    [HomeRequest requestSaveCardInfo:dicInfo success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            weakSelf.pointType = @"2";
            weakSelf.endTime = [BaseService getTimeStringSince1970];
            [weakSelf requestUploadPointType];
//            [weakSelf.navigationController popViewControllerAnimated:YES];
          
            [weakSelf dismissViewControllerAnimated:YES completion:^{
                if(weakSelf.confirmBlock){
                    weakSelf.confirmBlock(weakSelf.arrayInfo);
                }
            }];
           
        }
        xhl_showMessage(response[@"shared"]);
    
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        xhl_hudDismiss();
    }];
    
}

- (IBAction)clickCancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
